package com.balabala.gym_goods.service.impl;

import com.balabala.gym_goods.mapper.CoachScheduleMapper;
import com.balabala.gym_goods.pojo.dto.CoachScheduleDto;
import com.balabala.gym_goods.pojo.dto.ScheduleAddDto;
import com.balabala.gym_goods.pojo.entity.CoachSchedule;
import com.balabala.gym_goods.pojo.vo.CourseAndCoachVo;
import com.balabala.gym_goods.service.CoachScheduleService;
import com.balabala.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author fsj
 * @date 2023/5/5 18:20
 */
@Service
public class CoachScheduleServiceImpl extends ServiceImpl<CoachScheduleMapper, CoachSchedule> implements CoachScheduleService {

    @Override
    public Result addCoachSchedule(CoachScheduleDto coachScheduleDto) {
        CoachSchedule coachSchedule = new CoachSchedule();
        BeanUtils.copyProperties(coachScheduleDto, coachSchedule);
        int insert = getBaseMapper().insert(coachSchedule);
//        //先查询出刚刚添加的叫教练id
//        QueryWrapper<CoachSchedule> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("name", coachScheduleDto.getCoachName());
//        List<CoachSchedule> coaches = getBaseMapper().selectList(queryWrapper);
//
//        //添加教练对应的场馆
//        CoachUpdateByIdsDto coachUpdateByIdsDto = new CoachUpdateByIdsDto();
//        //可能查出多条最后一条数据
//        Long id = coaches.get(coaches.size() - 1).getId();
//        System.out.println(id);
//        coachUpdateByIdsDto.setCoachId(id);
//        coachUpdateByIdsDto.setVenueIds(coachDto.getVenueIds());
//        this.coachUpdateByIds(coachUpdateByIdsDto);
//

        if (insert > 0) {
            return Result.ok();
        }
        return Result.fail();
    }

    @Override
    public Boolean updateById(CoachScheduleDto coachScheduleDto) {
        return null;
    }

//    @Override
//    public Result addSchedule(ScheduleAddDto scheduleAddDto) {
//        CoachSchedule coachSchedule = new CoachSchedule();
//        BeanUtils.copyProperties(scheduleAddDto, coachSchedule);
//        int insert = getBaseMapper().insert(coachSchedule);
//
//        //TODO ....
//
//        if (insert > 0) {
//            return Result.ok();
//        }
//        return Result.fail();
//    }

    /**
     * 查询可预约课程时间段
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findAllTimeById(Long id) {
        CoachSchedule coachSchedule = getBaseMapper().selectById(id);

        //转换格式
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String startFormat = simpleDateFormat.format(coachSchedule.getStartTime());
        String endFormat = simpleDateFormat.format(coachSchedule.getEndTime());
        String str = startFormat;
        //处理开始时间
        String[] split = str.split(" ");
        String str1 = split[1];
        String[] split1 = str1.split(":");
        Integer a = Integer.valueOf(split1[0]);
        //处理结束时间
        String end = endFormat;
        String[] time = end.split(" ");
        String time1 = time[1];
        String[] time2 = time1.split(":");
        Integer b = Integer.valueOf(time2[0]);
        Integer time3 = b - a;
        List<Integer> list = new ArrayList<>();
        list.add(a);
        if (time3 > 1) {

            for (int i = 1; i < time3; i++) {
                list.add(a + i);
            }
            list.add(b);
        }
        System.out.println(list);
        return list;
    }


    public boolean addSchedule(ScheduleAddDto scheduleAddDto) {
        CoachSchedule coachSchedule=new CoachSchedule();
        BeanUtils.copyProperties(scheduleAddDto,coachSchedule);
        return save(coachSchedule);
    }

    @Override
    public List<CourseAndCoachVo> findCourseAndCoach() {
        return getBaseMapper().findCourseAndCoach();
    }
}
